package Servlets.lhl;/**
 * 项目名称：ProsuppbyidServlet.java
 * 文件名称：ProsuppbyidServlet.java
 * 类的描述：无
 * 创建时间：2025-07-25 18:37:33
 * 创建人：大帅比
 * <p>
 * 版权声明：本文件归 大帅比 所有，未经授权不得进行商业使用。
 */
 /*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            佛祖保佑       永无BUG
*/

import java.io.*;
import java.util.List;

import Bean.Product;
import Util.GokJDBCUtil2;
import Util.RES;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@WebServlet(name = "ProsuppbyidServlet", value = "/prosuppbyid")
public class ProsuppbyidServlet extends HttpServlet {
    public void init() {
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
        String id = req.getParameter("id");
        if (id != null) {
            //转成 int
            Integer id1 = Integer.parseInt(id);
            Product product = GokJDBCUtil2.queryForObject("select name as gsname from supplier where id=?", Product.class, id1);
            req.setAttribute("gsname", product.getGsname());
            String sql = "select id,proname," +
                    "price,originplace,quantity," +
                    "(select levelname from product_level where product_level.id=levelid ) " +
                    "as levelname,(select categoryname from tea_category where tea_category.id=categoryid) " +
                    "as categoryname,productionyear,productionmonth,status,levelid,categoryid,src,description from product where suppid = ? and isdeleted=0 order by id desc ";
            List<Product> products = GokJDBCUtil2.queryAll(sql, Product.class, id1);
            req.setAttribute("list", products);
            req.getRequestDispatcher("pages/promanger.jsp").forward(req, res);
        } else {
            res.getWriter().write(RES.fail("参数错误"));
        }
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String method = req.getMethod();
        try {
            if (method.equals("GET")) {
                this.doPost(req, res);
            } else {
                PrintWriter writer = res.getWriter();
                writer.println("{\"code\":\"400\",\"msg\":\"此方法不允许\"}");
                writer.close();
            }
        } catch (Exception e) {
            log.error("服务器处理请求时发生错误", e);
            PrintWriter writer = res.getWriter();
            writer.println("{\"code\":\"500\",\"msg\":\"服务器处理请求时发生错误\"}");
            writer.close();
        }
    }

    public void destroy() {
    }
}